package dao.server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;

public class TCPServer {
	/**
	 * @param args
	 */
	private static ServerSocket wellcomeSocket;
	private static DAO dao;
	private static int port;

	public static void main(String[] args) throws Exception {
		dao = new DAO();
		BufferedReader input = new BufferedReader(new InputStreamReader(
				System.in));
		while (true) {
			while (true) {
				System.out.print("Enter port number connect to database: ");
				try {
					dao.setPort(Integer.parseInt(input.readLine()));
					break;
				} catch (Exception e) {
					System.out.println("Port Error, please login again!");
					continue;
				}
			}
			System.out.print("Enter username: ");
			dao.setUsername(input.readLine());
			System.out.print("Enter password: ");
			dao.setPassword(input.readLine());
			if (!dao.getConnection()) {
				System.out
						.println("Can't connect to database, please try again!");
			} else {
				System.out.println("Login success");
				System.out.print("Enter port number to listen: ");
				try {
					port = Integer.parseInt(input.readLine());
					wellcomeSocket = new ServerSocket(port);
					break;
				} catch (Exception e) {
					System.out.println("Port Error, please login again!");
					continue;
				}
			}
		}
		Communicate com = new Communicate(wellcomeSocket, dao);
		com.start();
		String sentence = "";
		while (true) {
			sentence = input.readLine();
			if (sentence.toUpperCase().equals("ADD"))
				createUser(input);
			if (sentence.toUpperCase().equals("DELETE"))
				deleteUser(input);
		}
	}

	protected void finalize() {
		try {			
			wellcomeSocket.close();
			System.out.println("Close success");
			dao.serverDown();
			dao.closeConnection();
		} catch (Exception e) {
			System.out.println("Could not close socket");
		}
	}

	private static void createUser(BufferedReader input) {
		System.out.println("ADD NEW USER");
		User user = new User();
		String confirm = "";
		while (true) {
			try {
				System.out.print("Enter username: ");
				user.setUsername(input.readLine());
				System.out.print("Enter password: ");
				user.setPassword(input.readLine());
				System.out.print("Confirm password: ");
				confirm = input.readLine();
				if (confirm.equals(user.getPassword()))
					if (dao.addUser(user)) {
						System.out.println("Add user " + user.getUsername() + " success");
						break;
					} else {
						System.out.println("Can't add new user");
						System.out.println(user.getUsername()
								+ " already exist");
						continue;
					}
				System.out.println("Can't add new user");
			} catch (Exception e) {
				System.out.println("Can't add new user");
			}
		}
	}

	private static void deleteUser(BufferedReader input) {
		User user = new User();
		System.out.println("DELETE USER");
		try {
			System.out.print("Enter username: ");
			user.setUsername(input.readLine());
			if(dao.getUser(user) == null){
				System.out.println("Can't found user " + user.getUsername());
				return;
			}
			System.out.println("Are you sure delete user " + user.getUsername() + "?");
			System.out.print("Answer by yes/no: ");
			if(input.readLine().toUpperCase().equals("YES"))
				if(dao.deleteUser(user))
					System.out.println("Delete user " + user.getUsername() + " success");
				else
					System.out.println("Can't delete user " + user.getUsername());
			else{
				System.out.println("Can't delete user " + user.getUsername());
			}
		} catch (Exception e) {
			System.out.println("Can't delete user " + user.getUsername());
		}
	}
}
